﻿using System;
using System.Collections.Generic;
using SAPbouiCOM.Framework;
 
namespace ApexInterest
{
    
   
    class Program
    {
      
       // [STAThread]
        
        static void Main(string[] args)
        {
          
            Application oApp = null;
            if (args.Length < 1)
            {
                oApp = new Application();
            }
            else
            {
                oApp = new Application(args[0]);
            }

            Function.Company = Application.SBO_Application.Company.GetDICompany() as SAPbobsCOM.Company;

            if (Function.Company.Connected)
            {
                Application.SBO_Application.SetStatusBarMessage("Addon started", SAPbouiCOM.BoMessageTime.bmt_Short, false);

                Application.SBO_Application.MenuEvent += new SAPbouiCOM._IApplicationEvents_MenuEventEventHandler(SBO_Application_MenuEvent);

                Application.SBO_Application.AppEvent += new SAPbouiCOM._IApplicationEvents_AppEventEventHandler(SBO_Application_AppEvent);
                oMenu = new MainMenu();
                oMenu.LoadFromXML("MyMenus.xml", Application.SBO_Application);
                 CoFinancial oCoFinancial;
                oCoFinancial = new CoFinancial(Function.Company, Application.SBO_Application);


                oApp.Run();
                Function.Company.Disconnect();
                System.Windows.Forms.Application.Exit();

            }
            else
            {
                Application.SBO_Application.SetStatusBarMessage("Addon failed to connect Company", SAPbouiCOM.BoMessageTime.bmt_Short, true);
                System.Windows.Forms.Application.Exit();
            }
        }

        static void SBO_Application_MenuEvent(ref SAPbouiCOM.MenuEvent pVal, out bool BubbleEvent)
        {
           BubbleEvent = true;
           oMenu = new MainMenu();
           
            if (pVal.BeforeAction == true)
            {
                if (pVal.MenuUID == "MySubMenu01")
                {
                    oMenu.LoadFromXML("CoFinancialDeal.srf", Application.SBO_Application);
                    SAPbouiCOM.Form oForm;
                    oForm = Application.SBO_Application.Forms.Item("ABCOFINANCE");
                    oForm.Mode = SAPbouiCOM.BoFormMode.fm_ADD_MODE;
                    oMenu = new MainMenu();
                    oMenu.Bind(oForm, Application.SBO_Application,Function.Company);
                }
            }
            
        }

        static void SBO_Application_AppEvent(SAPbouiCOM.BoAppEventTypes EventType)
        {
            switch (EventType)
            {
                case SAPbouiCOM.BoAppEventTypes.aet_CompanyChanged:
                case SAPbouiCOM.BoAppEventTypes.aet_LanguageChanged:
                case SAPbouiCOM.BoAppEventTypes.aet_ServerTerminition:
                case SAPbouiCOM.BoAppEventTypes.aet_ShutDown:
                    System.Windows.Forms.Application.Exit();
                    break;
            }
        }
        private void LoadFromXML1()
        {
        }




        public static MainMenu oManu { get; set; }

        public static MainMenu oMenu { get; set; }
    }
}
